//
//  XQHomeBigApplyingView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/19.
//

#import "XQHomeBigApplyingView.h"
#import "XQHomeBigCardApplyingCell.h"

@interface XQHomeBigApplyingView()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic,copy) NSArray *images;
@property (nonatomic,strong) UILabel *applyingLb;
@property (nonatomic,strong) UICollectionView *collectionView;
@end

@implementation XQHomeBigApplyingView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.applyingLb];
    [self addSubview:self.collectionView];
}

- (void)makeConstraints {
    [self.applyingLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@16);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.applyingLb.mas_bottom).offset(10);
        make.left.equalTo(@16);
        make.right.equalTo(@-16);
        make.height.mas_equalTo(88);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark - setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegateFlowLayout,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XQHomeBigCardApplyingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XQHomeBigCardApplyingCell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataSource[indexPath.item];
    [cell setImageName:dic[@"inc"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.item];
    NSString *url = dic[@"aviation"];
    if (url.length) {
        if (self.didSelectHandle) {
            self.didSelectHandle(url);
        }
    }
}

#pragma mark - lazy

- (UILabel *)applyingLb {
    if (!_applyingLb) {
        _applyingLb = [[UILabel alloc] init];
        _applyingLb.text = @"Before applying for any loan";
        _applyingLb.textColor = [UIColor colorWithHexString:@"#0E1D31"];
        _applyingLb.font = [UIFont boldSystemFontOfSize:16];
    }
    return _applyingLb;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsZero;
        layout.itemSize = CGSizeMake([UIScreen mainScreen].width - 32, 88);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
       _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.automaticallyAdjustsScrollIndicatorInsets = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.translatesAutoresizingMaskIntoConstraints = NO;
        [_collectionView registerClass:[XQHomeBigCardApplyingCell class] forCellWithReuseIdentifier:@"XQHomeBigCardApplyingCell"];
    }
    return  _collectionView;
}

@end
